package com.yhp.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.managed.BasicManagedDataSource;

import java.sql.*;
import java.util.List;
import java.util.ResourceBundle;

public class Dbutils {
    private Connection connection;
    private PreparedStatement pps;
    private ResultSet resultSet;
    private static String driver;

    private int count ;
    /**DBCP链接数据池**/
    //private static BasicDataSource bds = new BasicDataSource();

    private static String name ;
    private static String pass ;
    private static String url ;

    /**C3P0链接数据吃*/
    //private ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();

    /**德鲁伊链接数据吃*/
    private static DruidDataSource druidDataSource = new DruidDataSource();


    //加载驱动
    static {
        /*try {
            ResourceBundle bundle = ResourceBundle.getBundle("db");
            driver = bundle.getString("driver");
            url = bundle.getString("url");
            name = bundle.getString("userName");
            pass = bundle.getString("passWord");


            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }*/

       /* ResourceBundle bundle = ResourceBundle.getBundle("db");
        driver = bundle.getString("driver");
        url = bundle.getString("url");
        name = bundle.getString("userName");
        pass = bundle.getString("passWord");

        bds.setUsername(name);
        bds.setPassword(pass);
        bds.setUrl(url);
        bds.setDriverClassName(driver);*/

        //德鲁伊
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driver = bundle.getString("driver");
        url = bundle.getString("url");
        name = bundle.getString("userName");
        pass = bundle.getString("passWord");
        druidDataSource.setUsername(name);
        druidDataSource.setUrl(url);
        druidDataSource.setPassword(pass);
        druidDataSource.setDriverClassName(driver);



    }

    //建立连接
    protected Connection getConnection(){
        try {
            connection = druidDataSource.getConnection();
            //connection = comboPooledDataSource.getConnection();
            //connection = bds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //创建状态通道
    protected PreparedStatement getPps(String sql){
        try {
            pps = getConnection().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pps;
    }

    //绑定参数
    protected void param(List list){
        if (list != null && list.size()>0) {
            for (int i = 0; i < list.size(); i++) {
                try {
                    pps.setObject(i + 1, list.get(i));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //增删改查
    protected int update (String sql,List list){
        getPps(sql);
        param(list);
        try {
            count = pps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;

    }

    //查询
    protected ResultSet query(String sql,List list){
        getPps(sql);
        param(list);
        try {
            resultSet = pps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    //关闭close
    protected void closeall(){
        try {
            if (connection != null) {
                connection.close();
            }
            if (pps != null) {
                pps.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
